package com.sl.discovery.modular.registry;

import com.sl.discovery.core.config.LoadbalanceConfiguration;
import com.sl.discovery.modular.DatabaseManager;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xiaojing
 * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
        matchIfMissing = true)
@AutoConfigureAfter({AutoServiceRegistrationConfiguration.class,
        AutoServiceRegistrationAutoConfiguration.class, LoadbalanceConfiguration.class})
public class DbServiceRegistryAutoConfiguration {

    @Bean
    public DbServiceRegistry dbServiceRegistry(DatabaseManager databaseManager) {
        return new DbServiceRegistry(databaseManager);
    }

    @Bean
    public DbRegistration dbRegistration(ApplicationContext context, InetUtils inetUtils) {
        return new DbRegistration(context,inetUtils);
    }

    @Bean
    public DbAutoServiceRegistration dbAutoServiceRegistration( DbServiceRegistry registry,AutoServiceRegistrationProperties autoServiceRegistrationProperties,
            DbRegistration registration) {
        return new DbAutoServiceRegistration(registry,
                autoServiceRegistrationProperties, registration);
    }

}
